package viewcontroller;

import utils.CategoryByStore;
import utils.ServiceHandler;
import model.Product;

import com.example.lastprojectbeautiplus.R;
import com.google.gson.Gson;

import adapter.ProductGridViewAdapter;
import android.app.Fragment;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemClickListener;

public class AllProductOnStoreFragment extends Fragment {
	public AllProductOnStoreFragment() {
		// TODO Auto-generated constructor stub
	}

	private Spinner spCategory;
	private GridView gvCategory;
	private ProductGridViewAdapter pAdapter;
	private Product[] productList;
	private CategoryByStore[] category;
	private String[] categoryName;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(
				R.layout.fragment_show_all_product_on_store, container, false);
		Bundle extras = getActivity().getIntent().getExtras();
		int target = extras.getInt("FromStoreView");
		spCategory = (Spinner) rootView.findViewById(R.id.spCategory);
		gvCategory = (GridView) rootView.findViewById(R.id.gvCategory);
		DisplayMetrics metrics = getResources().getDisplayMetrics();
		int height = metrics.heightPixels;
		spCategory.getLayoutParams().height = height / 17;
		new getCategoryOfStore(target).execute();
		spCategory.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				String urlProductByCate = "http://services-beautyplus.rhcloud.com/rest/ProductServices/ProByCate/"
						+ category[position].getCategoryID();
				new getProductList(urlProductByCate).execute();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
			}
		});
		gvCategory.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), SlideMenu.class);
				intent.putExtra("FromProductList",
						productList[position].getProductID());
				getActivity().startActivity(intent);
			}
		});
		return rootView;
	}

	private class getProductList extends AsyncTask<Void, Void, Void> {
		private String url;
		private String responseProductList;

		public getProductList(String url) {
			// TODO Auto-generated constructor stub
			this.url = url;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			ServiceHandler sh = new ServiceHandler();
			responseProductList = sh.executeGetRequest(url);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			Gson myGson = new Gson();
			productList = null;
			productList = myGson.fromJson(responseProductList, Product[].class);
			pAdapter = new ProductGridViewAdapter(getActivity(),
					R.layout.gridview_product_item, productList);
			gvCategory.setAdapter(pAdapter);
		}
	}

	private class getCategoryOfStore extends AsyncTask<Void, Void, Void> {
		int storeID;
		String respone;

		public getCategoryOfStore(int storeID) {
			// TODO Auto-generated constructor stub
			this.storeID = storeID;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			ServiceHandler sh = new ServiceHandler();
			String url = "http://services-beautyplus.rhcloud.com/rest/ProductServices/CateByStore?storeID="
					+ storeID;
			respone = sh.executeGetRequest(url);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			Gson categoryGson = new Gson();
			spCategory = (Spinner) getActivity().findViewById(R.id.spCategory);
			category = categoryGson.fromJson(respone, CategoryByStore[].class);
			categoryName = new String[category.length];
			for (int i = 0; i < category.length; i++)
				categoryName[i] = category[i].getCategoryName();
			ArrayAdapter<String> spAdapter = new ArrayAdapter<String>(
					getActivity(),
					android.R.layout.simple_spinner_dropdown_item, categoryName);
			spAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spCategory.setAdapter(spAdapter);
			super.onPostExecute(result);
		}
	}
}
